package com.qf.lanzhou.blog.api.controller;

import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.UpdatePasswordService;
import com.qf.lanzhou.blog.api.service.impl.UpdatePasswordServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author jian   个人中心修改密码
 * @Date 2020/12/17 17:05
 * @JDK 1.8
 * @Version 1.0
 */
@WebServlet("/updatepassword")
public class UpdatePasswordServlet extends BaseServletPost{
    private UpdatePasswordService updatePasswordService=new UpdatePasswordServiceimpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决请求的乱码
        req.setCharacterEncoding("utf-8");
        //解决响应乱码
        resp.setContentType("text/html;charset=utf-8");

        //获取参数
        User user1 = (User) req.getSession().getAttribute("user");
        Long id = user1.getId();

        String userName = req.getParameter("userName");
        String password = req.getParameter("password");

        //判断用户是否有登录
        Object user = req.getSession().getAttribute("user");
        if(user==null){
            failedResultWrite("还未登录",resp);
        }
        //查询用户名和密码是否存在
        List<User> list=updatePasswordService.selectUsernameAndPassword(userName,password,id);
        for (int j = 0; j <list.size(); j++) {
            User user2 = list.get(j);
            System.out.println(user2);

            if(!userName.equals(user2.getUserName())){
                failedResultWrite("用户名输入不正确",resp);
                if(password.equals(user2.getPassword())){
                    failedResultWrite("密码输入不正确",resp);
                }
            }
        }
        //修改密码
        int i=updatePasswordService.update(password,id);
        if(i==1){
            successResultWrite("修改成功",resp);
        }
    }
}
